<script type="text/javascript">
	function HomePageCtrl(CtrlUtil, $http, modalService, $q, $controller,
		$scope, $rootScope, $state) {
		var ctrl = this
		ctrl.display = 'row'
		ctrl.pageSize = 9
		ctrl.typeQueryParam = ''
		//CtrlUtil.build(ctrl, ':root/course/getCourses')
		ctrl.query = undefined;
		this.changeType = function (type) {
			ctrl.typeQueryParam = type
			//ctrl.query()
		}
		$scope.$on('courseSearch', function (e, data) {
			ctrl.nameQueryParam = data.name
			ctrl.fuzzyQueryParam = data.fuzzy
			ctrl.query()
		})
		this.toImageUrl = CtrlUtil.toImageUrl
		$http.post(':root/course/time').success(function (r) {
			ctrl.time = r
			return r
		})
		this.preview = function (item) {
			var title = '课程详情'
			var result = item
			if (item) {
				result.imageUrl = CtrlUtil.toImageUrl(item.tpId, 1)
				result.status = (item.status == 1 || ctrl.time >= item.kcsj) ? 1
					: 0
			}
			modalService.show({
				title: title,
				contentUrl: ':root/components/common/courseView.html',
				size: 'lg',
				titleCls: '',
				result: result,
				ctrl: ctrl
			})
		}
		this.apply = function (item) {
			if (item.status == 1 || ctrl.time >= item.kcsj) {
				return;
			}
			if ($rootScope.$user.type < 0) {
				modalService
					.error('请您<a href="javascript:;" ng-controller="LoginCtrl as ctrl" ng-click="ctrl.switchLogin()">登录</a>后再进行操作')
				return;
			}
			$http.post(':root/course/apply', {
				courseId: item.id
			}).then(function (response) {
				var r = response.data
				if (r == 'succ') {
					modalService.succ('报名成功', 1500)
					ctrl.course.queryPage()
				} else {
					modalService.error(r, 1500)
				}
			})
		}
		var outterCtrl
		if ($scope.courseDisplayCtrl) {
			outterCtrl = $scope.courseDisplayCtrl
		}
		if (!outterCtrl && $scope.courseDisplayCtrlName) {
			outterCtrl = $controller($scope.courseDisplayCtrl, $scope)
		}
		ctrl.getTerm = function () {

		}
		ctrl.queryUrl = ':root/course/getCourses'
		outterCtrl && angular.extend(this, outterCtrl)
		if (!this.query) {
			this.query = function () {
				ctrl.entities && (ctrl.entities.content = {})
				var p = CtrlUtil.getQueryParams(ctrl)
				$http.post(ctrl.queryUrl, p).success(function (r) {
					var ids = CtrlUtil.getIds(r.content)
					$q.all($http.post(':root/course/getApplyStatus', {
						ids: ids
					}).success(function (R) {
						var map = {}
						angular.forEach(R, function (v) {
							map[v[0]] = v[1]
						})
						angular.forEach(r.content, function (v) {
							v.applyed = map[v.id] && (map[v.id] > 0)
						})
						ctrl.entities = r;
					}), $http.post(':root/course/getCourseApplyCount', {
						ids: ids
					}).success(function (R) {
						var map = {}
						angular.forEach(R, function (v) {
							map[v[0]] = v[1]
						})
						angular.forEach(r.content, function (v) {
							v.applyCount = map[v.id] || 0
						})
					}), $http.post(':root/trainee/getByXh', {
						xh: $rootScope.$user.uid
					}).success(function (r) {
						return $rootScope.$signUpInfo = r || {}
					}))
				})
			}
		}
		this.onStuLoad = function (stuInfo) {
			var map = this.termMap = {};
			angular.forEach(stuInfo, function (v) {
				map[v.term.id] = v;
			})
		}
		this.onCourseLoad = function (data) {
			this.courseList = data;
		}
		this.onMyCourseLoad = function (data) {
			var list = this.courseList = []
			angular.forEach(data, function (v) {
				v.courseInfo.applys = v.applys
				list.push(v.courseInfo)
			})
		}
	}

</script>
<div ng-controller="HomePageCtrl as ctrl" ng-init="ctrl.changeType('')">
	<div class="container-fluid">
		<data entity="CourseInfo" auto-load="!$ctrl.myCourse" page-size="9" bind-ctrl="ctrl.course" after-refresh="ctrl.onCourseLoad(data)">
			<filter enable="ctrl.typeQueryParam" field="kclb" value="ctrl.typeQueryParam"></filter>
			<data entity="CourseApplyID" slave-as="applys" slave-property="courseInfo_id" master-property="id"></data>
			<data filter-master="$root.$user.type==0&&$ctrl.filter" filter-only="true" entity="StuInfo" slave-property="term.id" master-property="term.id"
			 page="false" after-refresh="stuInfos=(data)">
				<filter field="user.id" value="$root.$user.id"></filter>
			</data>
			<order field="fbsj" type="desc"></order>
		</data>
		<data auto-load="$ctrl.myCourse" entity="CourseApply" page-size="9" bind-ctrl="ctrl.mycourse" after-refresh="ctrl.onMyCourseLoad(data)">
			<filter field="stuInfo.user.id" value="$root.$user.id"></filter>
			<filter enable="ctrl.typeQueryParam" field="courseInfo.kclb" value="ctrl.typeQueryParam"></filter>
			<data entity="CourseApplyID" slave-as="applys" slave-property="courseInfo_id" master-property="courseInfo.id"></data>
			<order field="courseInfo.fbsj" type="desc"></order>
		</data>
		<data entity="StuInfo" page="false" auto-load="true" after-refresh="ctrl.onStuLoad(data)">
			<filter field="user.id" value="$root.$user.id"></filter>
		</data>
		<div class="row media">
			<div class="col-md-6">
				<div class="row">
					<ul class="nav nav-tabs">
						<li role="presentation" ng-click="ctrl.changeType('')" ng-class="{active:ctrl.typeQueryParam==''}"><a href="javascript:;">最新课程</a></li>
						<li role="presentation" ng-click="ctrl.changeType('1')" ng-class="{active:ctrl.typeQueryParam=='1'}"><a href="javascript:;">必修课</a></li>
						<li role="presentation" ng-click="ctrl.changeType('2')" ng-class="{active:ctrl.typeQueryParam=='2'}"><a href="javascript:;">选修课</a></li>
						<li role="presentation" ng-click="ctrl.changeType('3')" ng-class="{active:ctrl.typeQueryParam=='3'}"><a href="javascript:;">体验课</a></li>
					</ul>
				</div>
			</div>
			<div class="col-md-6 text-right">
				<div ng-click="ctrl.display='row'" class="btn btn-sm btn-success">单列</div>
				<div ng-click="ctrl.display='block'" class="btn btn-sm btn-primary">九宫格</div>
			</div>
		</div>
		<div class="row media">
			<div class="col-md-12">
				<ul class="nav nav-pills" ng-show="$ctrl.filter">
					<li ng-click="" role="presentation" ng-repeat="s in stuInfos"><a href="javascript:;">{{s.term.trainType.name}}</a></li>
				</ul>
			</div>
		</div>
		<div class="row media">
			<div ng-if="ctrl.display=='row'">
				<div class="col-md-12 thumbnail panel-body" ng-repeat="item in ctrl.courseList">
					<div class="col-md-4 ">
						<div class="row">
							<img class="col-md-12" class=" " ng-src="{{ctrl.toImageUrl(item.tpId,1)}}">
						</div>
					</div>
					<div class="col-md-8">
						<div class="col-md-12" style="padding-left: 20px;">
							<div class="row">
								<label class="col-xs-12 control-label">
									<h4>
										<span class="glyphicon glyphicon-paperclip"></span>
										{{item.name}} <small class="pull-right">{{item.kclb|courseType}}</small>
									</h4>
								</label>

							</div>
							<div class="row">
								<label class="col-md-2 control-label"> <span
									class="glyphicon glyphicon-user"></span> 主讲人
								</label>
								<div class="col-md-3">
									<p>{{item.zjr}}</p>
								</div>
								<label class="col-md-2 control-label"> <span
									class="glyphicon glyphicon-dashboard"></span> 开课时间
								</label>
								<div class="col-md-5">
									<p>{{item.kcsj|date:'yyyy-MM-dd HH:mm EEE'}}</p>
								</div>
							</div>
							<div class="row"></div>
							<div class="row">
								<label class="col-md-2 control-label"> <span
									class="glyphicon glyphicon-map-marker"></span> 地点
								</label>
								<div class="col-md-10">
									<p>{{item.kkdd}}</p>
								</div>
							</div>
							<div class="row">
								<label class="col-md-2 control-label"> <span
									class="glyphicon glyphicon-list-alt"></span> 简介
								</label>
								<div class="col-md-10" ng-class="{'brief-small':briefSmall,brief:!briefSmall}">
									<p title="{{item.kcjj}}">{{item.kcjj}}</p>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<div class="pull-left form-control-static">
										<label class="control-label">&nbsp;</label> <span>课程名额:{{item.kcme}}</span>
										<span class="margin-left">|</span> <span class="margin-left">已选:{{item.applys.length||0}}</span>
									</div>
									<div class="pull-right">
										<data entity="CourseApply" auto-load="$root.$user.type==0" after-refresh="myApply=data">
											<filter field="stuInfo.user.uid" value="$root.$user.id"></filter>
											<filter field="courseInfo.id" value="item.id"></filter>
										</data>
										<a class="btn btn-default" ng-click="ctrl.preview(item)">课程详情</a>
										<span ng-show="myApply.length>0&&$root.$user.type==0">
											<a disabled="disabled" class="btn btn-info">{{(item.status == 1 || $root.currentTime >= item.kcsj)?'已授课':'已报名'}}</a>
										</span>
										<span ng-show="myApply.length==0&&$root.$user.type==0&&($ctrl.filter||ctrl.termMap[item.term.id])">
											<a  ng-show="(item.applys.length||0)>=item.kcme" ng-disabled="1" class="btn btn-cys">
												 {{(item.status == 1 || ctrl.time >= item.kcsj)?'已过期':'名额已满'}}</a>
											<a ng-show=" ((item.applys.length||0)<item.kcme)" ng-disabled="(item.status == 1 || ctrl.time >= item.kcsj)" ng-click="ctrl.apply(item)" class="btn btn-cys">
												{{(item.status 	== 1 || ctrl.time >= item.kcsj)?'已过期':'马上报名'}}</a>
										</span>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div ng-if="ctrl.display=='block'" class="row">
				<div class="col-xs-4 panel-body" ng-repeat="item in  ctrl.courseList">
					<div class="row">
						<div class="row" ng-click="ctrl.preview(item)">
							<img style="cursor: pointer;" class="col-xs-12" class=" " ng-src="{{ctrl.toImageUrl(item.tpId,1)}}">
							<div class="panel-body" style="position: absolute; right: 0; padding-top: 3px;">
								<span class="label label-default">{{item.kclb|courseType}}</span>
							</div>
						</div>


						<div class="col-md-12 panel-body">
							<label>{{item.name}}</label> <span class="pull-right" title="开课时间">{{item.kcsj|date:'yyyy-MM-dd HH:mm'}}</span>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<pager ng-show="!$ctrl.myCourse" class="pull-right panel-body" bind-ctrl="ctrl.course"></pager>
				<pager ng-show="$ctrl.myCourse" class="pull-right panel-body" bind-ctrl="ctrl.mycourse"></pager>
			</div>
		</div>
	</div>
</div>